package gui;

import constants.UserTypes;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import controllers.GUIController;
import hibernate.User;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import java.util.Date;

@SuppressWarnings("serial")
public class AAddUser extends JInternalFrame {

    private javax.swing.JButton addUserButton;
    private javax.swing.JPanel bkgrd;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField firstNameField;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JTextField lastNameField;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JLabel logo;
    private javax.swing.JPanel userInfoPanel;
    private GUIController guiC;
    final AAddUser obj = this;
    private JPasswordField loginPass;
    private JTextField usr;

    public AAddUser(GUIController m, JTextField usrName) {
        guiC = m;
        usr = usrName;
        generateLayout();
        addCancelActionListener();
        addUserActionListener();

    }

    //adds actionlistener to cancel button to go to the my account page
    private void addCancelActionListener() {
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new ALogin(guiC));
                obj.dispose();
            }
        });
    }

    // adds actionlistener to add student button creates the given user
    // and shows the my class page if new user creation is successful
    private void addUserActionListener() {
        addUserButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createUser();
                guiC.showGUI(new AMyClass(guiC));
                obj.dispose();
            }
        });
    }

    private void createUser() {
        User u = new User();
        u.setCommonName(firstNameField.getText());
        u.setFirstName(firstNameField.getText());
        u.setLastName(lastNameField.getText());
        u.setCreateDate(new Date());
        u.setImagePath(null);
        u.setUserAccess(UserTypes.TEACHER.VALUE());
        guiC.saveDbData(u);
        guiC.currentUser = u;
    }
    
    public void generateLayout() {
        bkgrd = new javax.swing.JPanel();
        logo = new javax.swing.JLabel();
        userInfoPanel = new javax.swing.JPanel();
        firstNameLabel = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        firstNameField = new javax.swing.JTextField(usr.getText());
        lastNameLabel = new javax.swing.JLabel();
        lastNameField = new javax.swing.JTextField();
        addUserButton = new javax.swing.JButton();

        bkgrd.setBackground(new java.awt.Color(0, 102, 204));

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/OnPointlogo2-1.png"))); // NOI18N

        userInfoPanel.setBackground(new java.awt.Color(51, 153, 255));
        userInfoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Teacher Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Aharoni", 1, 14))); // NOI18N

        firstNameLabel.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12)); // NOI18N
        firstNameLabel.setText("First Name: ");

        cancelButton.setText("Cancel");
        cancelButton.setOpaque(false);

        firstNameField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        lastNameLabel.setFont(new java.awt.Font("Tahoma", Font.BOLD, 12)); // NOI18N
        lastNameLabel.setText("Last Name: ");

        lastNameField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        addUserButton.setText("Add Teacher");
        addUserButton.setOpaque(false);


        javax.swing.GroupLayout userInfoPanelLayout = new javax.swing.GroupLayout(userInfoPanel);
        userInfoPanelLayout.setHorizontalGroup(
                userInfoPanelLayout.createParallelGroup(Alignment.TRAILING)
                .addGroup(userInfoPanelLayout.createSequentialGroup()
                .addGap(24)
                .addGroup(userInfoPanelLayout.createParallelGroup(Alignment.TRAILING)
                .addGroup(userInfoPanelLayout.createSequentialGroup()
                .addGroup(userInfoPanelLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(userInfoPanelLayout.createParallelGroup(Alignment.LEADING)
                .addComponent(firstNameLabel)
                .addComponent(lastNameLabel, Alignment.TRAILING)))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(userInfoPanelLayout.createParallelGroup(Alignment.TRAILING)
                .addGroup(Alignment.LEADING, userInfoPanelLayout.createParallelGroup(Alignment.LEADING, false)
                .addComponent(firstNameField, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                .addComponent(lastNameField))))
                .addComponent(addUserButton))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(cancelButton)
                .addContainerGap()));
        userInfoPanelLayout.setVerticalGroup(
                userInfoPanelLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(userInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(userInfoPanelLayout.createParallelGroup(Alignment.BASELINE)
                .addComponent(firstNameLabel)
                .addComponent(firstNameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(userInfoPanelLayout.createParallelGroup(Alignment.BASELINE)
                .addComponent(lastNameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(lastNameLabel))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(userInfoPanelLayout.createParallelGroup(Alignment.BASELINE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(userInfoPanelLayout.createParallelGroup(Alignment.BASELINE))
                .addPreferredGap(ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addGroup(userInfoPanelLayout.createParallelGroup(Alignment.BASELINE)
                .addComponent(cancelButton)
                .addComponent(addUserButton))));
        userInfoPanel.setLayout(userInfoPanelLayout);

        javax.swing.GroupLayout bkgrdLayout = new javax.swing.GroupLayout(bkgrd);
        bkgrdLayout.setHorizontalGroup(
                bkgrdLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addGroup(bkgrdLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logo))
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addGap(72)
                .addComponent(userInfoPanel, GroupLayout.PREFERRED_SIZE, 346, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(339, Short.MAX_VALUE)));
        bkgrdLayout.setVerticalGroup(
                bkgrdLayout.createParallelGroup(Alignment.TRAILING)
                .addGroup(bkgrdLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logo, GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(userInfoPanel, GroupLayout.PREFERRED_SIZE, 219, GroupLayout.PREFERRED_SIZE)
                .addGap(269)));
        bkgrd.setLayout(bkgrdLayout);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        layout.setHorizontalGroup(
                layout.createParallelGroup(Alignment.LEADING)
                .addComponent(bkgrd, GroupLayout.DEFAULT_SIZE, 757, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(Alignment.LEADING)
                .addComponent(bkgrd, GroupLayout.DEFAULT_SIZE, 605, Short.MAX_VALUE));
        getContentPane().setLayout(layout);

        pack();
    }
}
